// pages/myAllNoticeList/myAllNoticeList.js
let noticeModel = require('../../models/notice.js')
var bxt = require('../../utils/bxt.js')
var getNoticesOperation = require('../../operation/getNoticesOperation.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyNotices()
    this.getNotices()
    getApp().notificationCenter.register(bxt.REFRESH_NOTICES_MESSAGE, this, "getNotices");
    getApp().notificationCenter.register(bxt.DELETE_NOTICE_MESSAGE, this, "getMyNotices");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    getApp().notificationCenter.remove(bxt.REFRESH_NOTICES_MESSAGE, this)
    getApp().notificationCenter.remove(bxt.DELETE_NOTICE_MESSAGE, this)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getNotices()
  },

  /**
   * 点击添加通知事件处理函数
   */
  onCreateNotice:function() {
    getApp().globalData.param = null  // 避免进入创建通知界面，误认为是修改模式
    wx.navigateTo({
      url: '../editNotice/editNotice'
    })
  },

  /**
   * 点击通知Cell事件处理函数
   */
  onShowMessagePreview: function (event) {
    getApp().globalData.param = event.currentTarget.dataset.notice;
    wx.navigateTo({
      url: '../noticePreview/noticePreview',
    })
  },

  getMyNotices:function() {
    let notices = noticeModel.makeUINotices(noticeModel.getMyNotices(), 0)
    this.setData({
      notices: notices
    })
  },

  /**
   * 获取我创建的通知
   */
  getNotices: function () {
    let that = this

    getNoticesOperation.getNotices('created')
      .then(res => {
        noticeModel.setMyNotices(res)
        that.setData({
          notices: noticeModel.makeUINotices(res,0)
        })
      })
  },
})